package cn.maxmc.murderaddons.machine.machines

import cn.maxmc.murderaddons.machine.BaseMachine
import cn.maxmc.murderaddons.machine.MachineType
import cn.maxmc.murderaddons.task.TaskType
import io.izzel.taboolib.util.item.ItemBuilder
import io.izzel.taboolib.util.item.ItemStacker
import io.izzel.taboolib.util.item.inventory.MenuBuilder
import io.izzel.taboolib.util.lite.Materials
import me.arasple.mc.trmenu.api.TrMenuAPI
import me.arasple.mc.trmenu.api.factory.task.ClickTask
import org.bukkit.Location
import org.bukkit.Material
import org.bukkit.configuration.serialization.SerializableAs
import org.bukkit.entity.Player
import org.bukkit.event.inventory.InventoryType
import org.bukkit.inventory.ItemStack

@SerializableAs("PowerMachine")
class PowerMachine: BaseMachine {
    constructor(mainLocation: Location): super(MachineType.SINGLE_LOCATION, TaskType.COMMON, mainLocation)
    constructor(map: Map<String,Any>): super(map)

//    val button = ArrayList<Button>()

    override fun requestTask(player: Player, loc: Location) {
        Matcher(player).open()
    }

    class Matcher(val player: Player) {
        val menu = TrMenuAPI.buildMenu()
        val buttons_l = ArrayList<ItemStack>()
        val buttons_l_shiny = ArrayList<ItemStack>()
        val buttons_r = ArrayList<ItemStack>()
        val buttons_r_shiny = ArrayList<ItemStack>()

        init {
            val triangle = ItemBuilder(Materials.RED_STAINED_GLASS_PANE.parseMaterial())
                .name("§c△")
            val cross = ItemBuilder(Materials.BLUE_STAINED_GLASS_PANE.parseMaterial())
                .name("§9✕")
            val circle = ItemBuilder(Materials.YELLOW_STAINED_GLASS_PANE.parseMaterial())
                .name("§e〇")
            val star = ItemBuilder(Materials.MAGENTA_STAINED_GLASS_PANE.parseMaterial())
                .name("§d☆")

            val list = arrayListOf(
                triangle,
                cross,
                circle,
                star
            )

            val left = list.shuffled()
            buttons_l.addAll(left.map { it.build() })
            buttons_l_shiny.addAll(left.map { it.shiny().build() })
            val right = list.shuffled()
            buttons_r.addAll(right.map { it.build() })
            buttons_r_shiny.addAll(right.map { it.shiny().build() })

            left.forEachIndexed { index, itemBuilder ->
                menu.item("l${index}", itemBuilder.build())
            }
            right.forEachIndexed { index, itemBuilder ->
                menu.item("r${index}", itemBuilder.build())
            }
        }

        fun open() {

        }

        fun click() {

        }
    }

}